package com.yk.airline.component.analysisdata;

import com.alibaba.fastjson.JSONObject;
import com.yk.airline.component.analysisdata.api.ICommonAnalysis;
import com.yk.airline.dao.analysisdata.api.IAnalysisDataDao;
import com.yk.airline.dao.capturemark.api.ICaptureMarkDao;
import com.yk.airline.dao.origindata.api.IOriginDataDao;
import com.yk.airline.model.AnalysisDataModel;
import com.yk.airline.model.CaptureMarkModel;
import com.yk.airline.model.OriginDataModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Project Name:airlinecrawler
 * File Name:AnalysisDataUtil
 * Package Name:com.yk.airline.component.analysisdata
 * Date:2018/2/1 20:30
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@Component("analysisDataUtil")
public class AnalysisDataUtil {

    @Resource(name = "captureMarkDao")
    private ICaptureMarkDao captureMarkDao;
    @Resource(name = "xieChengDataAnalysis")
    private ICommonAnalysis xieChengDataAnalysis;
    @Resource(name = "originDataDao")
    private IOriginDataDao originDataDao;
    @Resource(name = "tongChengDataAnalysis")
    private ICommonAnalysis tongChengDataAnalysis;


    /**
     * 开始解析任务
     */
    public void startRecentDataAnalysis() {

        CaptureMarkModel theLatestMark = captureMarkDao.findTheLatestMark();
        System.out.println(theLatestMark.getId());
        System.out.println(theLatestMark.getCaptureTime());
        List<OriginDataModel> byCaptureMark = originDataDao.findByCaptureMark(theLatestMark);
        for (OriginDataModel item : byCaptureMark) {
            String arrivePlace = item.getAirLineByAirLine().getCityByArrivePlace().getName();
            String depPlace = item.getAirLineByAirLine().getCityByOriginPlace().getName();
            JSONObject airLine = new JSONObject();
            airLine.put("depart", depPlace);
            airLine.put("arrive", arrivePlace);
            if (item.getCaptureWebsiteByCaptureUrl().getName().equals("携程")) {
                Elements allParentElement = xieChengDataAnalysis.getAllParentElement(item.getHtml(), ".flight-list-content .search_box");
                System.out.println(allParentElement.size());
                String date = null;
                Elements dataElements = Jsoup.parse(item.getHtml()).select(".calendar_tab_list .current");
                if (dataElements.isEmpty()) {
                    System.out.println("携程航线:" + depPlace + "-" + arrivePlace + "html 数据不完整，无法完成解析...");
                    continue;
                } else {
                    date = dataElements.get(0).attr("date");
                    for (Element e : allParentElement) {
                        xieChengDataAnalysis.saveData(e, date, theLatestMark, airLine.toString());
                    }
                }
            } else if (item.getCaptureWebsiteByCaptureUrl().getName().equals("同程")) {
                Elements allParentElement = tongChengDataAnalysis.getAllParentElement(item.getHtml(), ".all-fight-result .flist_box");
                if (!allParentElement.isEmpty()) {
                    for (Element e : allParentElement) {
                        tongChengDataAnalysis.saveData(e, null, theLatestMark, airLine.toString());
                    }
                } else {
                    System.out.println("同程航线:" + depPlace + "-" + arrivePlace + "html 数据不完整，无法完成解析...");
                    continue;
                }
            }
        }

    }


}
